package org.nanhill.xmlro.sro.model;

import java.lang.reflect.Field;
import java.util.List;

import org.nanhill.commons.lang.ReflectKit;
import org.nanhill.xmlro.Serializer;
import org.nanhill.xmlro.XmlroException;
import org.nanhill.xmlro.XmlroOutput;

/**
 * Serializing an object for known object types.
 */
/**
 * @author alin
 * 
 */
public class JavaSerializer implements Serializer {
	public void writeObject(Object obj, XmlroOutput out) throws XmlroException {
		out.writeMapBegin();
		List<Field> fields = ReflectKit.getAllAccessibleField(obj);
		for (Field field : fields) {
			out.writeString(field.getName());
			try {
				out.writeObject(field.get(obj));
			} catch (IllegalArgumentException e) {
				throw new XmlroException(e);
			} catch (IllegalAccessException e) {
				throw new XmlroException(e);
			}
		}
		out.writeMapEnd();
	}
}
